package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.CurrentUser;
import cn.b.training.baby_education_training.entity.UserInfo;
import cn.b.training.baby_education_training.repository.BabyInfoRepository;
import cn.b.training.baby_education_training.repository.UserInfoRepository;
import cn.b.training.baby_education_training.service.UserInfoService;
import cn.b.training.baby_education_training.service.dto.ResultInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;

/**
 * @program: baby_education
 * @description
 * @author: wuying
 * @create: 2022-11-29 10:53
 **/
@Service
public class UserInfoServiceImpl implements UserInfoService {


    private final UserInfoRepository userInfoRepository;
    private final BabyInfoRepository babyInfoRepository;

    public UserInfoServiceImpl(UserInfoRepository userInfoRepository, BabyInfoRepository babyInfoRepository) {
        this.userInfoRepository = userInfoRepository;
        this.babyInfoRepository = babyInfoRepository;
    }

    @Override
    public ResultInfo logout(String phone){
        // 建立一个返回对象
        ResultInfo result=new ResultInfo();
        result.setSuccess(false);
        result.setUser(null);
        // 判断输入的用户名和登录的用户名是否一致
        UserInfo user1 = userInfoRepository.getByPhone(phone);
        if(user1.getPhone().equals(phone)){
            // 根据用户名查询用户
            UserInfo user = userInfoRepository.getByPhone(phone);
            // 判断该用户的状态码
            if(isStatus(user)){
                result.setMsg("退出状态!");
                return result;
            }
            //更改状态码
            userInfoRepository.changeStatus(user.getId(),"0");
            // 传入数据
            user.setStatus("0");
            result.setSuccess(true);
            result.setMsg("退出登录");
            result.setUser(user);
            return result;
        }
        result.setMsg("两个的用户名不一致");
        return result;
    }

    @Override
    public boolean isStatus(UserInfo user) {
        return "0".equals(user.getStatus());
    }


    @Override
    public UserInfo getByPhone(String phone) {
        return userInfoRepository.getByPhone(phone);
    }

    @Override
    public UserInfo getUser(String phone) {
        return userInfoRepository.getByPhone(phone);
    }

    @Override
    public UserInfo regAccount(String phone, String password, String nickname) throws Exception {
        //根据登录名查询账户是否存在
        UserInfo userInfo = getUser(phone);
        if(userInfo!=null){
            throw new Exception("登录名已存在，注册失败");
        }
        userInfo = new UserInfo();
        userInfo.setStatus("0");
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
        userInfo.setPassword(encoder.encode(password));
        userInfo.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        userInfo=this.userInfoRepository.save(userInfo);
        return userInfo;
    }

    @Override
    public Page<UserInfo> getAllAccounts(Integer page, Integer size) {
        String login = CurrentUser.CURRENTLOGIN.get();
        System.out.println("getAllAccounts login:"+login);
        return this.userInfoRepository.findAll(PageRequest.of(page,size));
    }

    @Transactional
    @Override
    public Boolean deleteAccount(String login) throws Exception {
        //根据登录名查询账户是否存在
        UserInfo account = getUser(login);
        if(account==null){
            throw new Exception("账户不存在");
        }
        try{
            this.userInfoRepository.deleteById(account.getId());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public int updatePhone(String phone,Long id){
        return userInfoRepository.updatePhone(phone,id);
    }


    //@Override
    //public int updateNickname(String nickname) {
    //    return userInfoRepository.updateNickname(nickname);
    //}


}
